const t=["top","right","bottom","left"],e=["start","end"],n=t.reduce(((t,n)=>t.concat(n,n+"-"+e[0],n+"-"+e[1])),[]),o=Math.min,r=Math.max,i={left:"right",right:"left",bottom:"top",top:"bottom"},a={start:"end",end:"start"};function l(t,e,n){return r(t,o(e,n))}function s(t,e){return"function"==typeof t?t(e):t}function c(t){return t.split("-")[0]}function f(t){return t.split("-")[1]}function u(t){return"x"===t?"y":"x"}function m(t){return"y"===t?"height":"width"}function g(t){return["top","bottom"].includes(c(t))?"y":"x"}function d(t){return u(g(t))}function p(t,e,n){void 0===n&&(n=!1);const o=f(t),r=d(t),i=m(r);let a="x"===r?o===(n?"end":"start")?"right":"left":"start"===o?"bottom":"top";return e.reference[i]>e.floating[i]&&(a=y(a)),[a,y(a)]}function h(t){return t.replace(/start|end/g,(t=>a[t]))}function y(t){return t.replace(/left|right|bottom|top/g,(t=>i[t]))}function x(t){return"number"!=typeof t?function(t){return{top:0,right:0,bottom:0,left:0,...t}}(t):{top:t,right:t,bottom:t,left:t}}function w(t){return{...t,top:t.y,left:t.x,right:t.x+t.width,bottom:t.y+t.height}}function v(t,e,n){let{reference:o,floating:r}=t;const i=g(e),a=d(e),l=m(a),s=c(e),u="y"===i,p=o.x+o.width/2-r.width/2,h=o.y+o.height/2-r.height/2,y=o[l]/2-r[l]/2;let x;switch(s){case"top":x={x:p,y:o.y-r.height};break;case"bottom":x={x:p,y:o.y+o.height};break;case"right":x={x:o.x+o.width,y:h};break;case"left":x={x:o.x-r.width,y:h};break;default:x={x:o.x,y:o.y}}switch(f(e)){case"start":x[a]-=y*(n&&u?-1:1);break;case"end":x[a]+=y*(n&&u?-1:1)}return x}const b=async(t,e,n)=>{const{placement:o="bottom",strategy:r="absolute",middleware:i=[],platform:a}=n,l=i.filter(Boolean),s=await(null==a.isRTL?void 0:a.isRTL(e));let c=await a.getElementRects({reference:t,floating:e,strategy:r}),{x:f,y:u}=v(c,o,s),m=o,g={},d=0;for(let n=0;n<l.length;n++){const{name:i,fn:p}=l[n],{x:h,y:y,data:x,reset:w}=await p({x:f,y:u,initialPlacement:o,placement:m,strategy:r,middlewareData:g,rects:c,platform:a,elements:{reference:t,floating:e}});f=null!=h?h:f,u=null!=y?y:u,g={...g,[i]:{...g[i],...x}},w&&d<=50&&(d++,"object"==typeof w&&(w.placement&&(m=w.placement),w.rects&&(c=!0===w.rects?await a.getElementRects({reference:t,floating:e,strategy:r}):w.rects),({x:f,y:u}=v(c,m,s))),n=-1)}return{x:f,y:u,placement:m,strategy:r,middlewareData:g}};async function A(t,e){var n;void 0===e&&(e={});const{x:o,y:r,platform:i,rects:a,elements:l,strategy:c}=t,{boundary:f="clippingAncestors",rootBoundary:u="viewport",elementContext:m="floating",altBoundary:g=!1,padding:d=0}=s(e,t),p=x(d),h=l[g?"floating"===m?"reference":"floating":m],y=w(await i.getClippingRect({element:null==(n=await(null==i.isElement?void 0:i.isElement(h)))||n?h:h.contextElement||await(null==i.getDocumentElement?void 0:i.getDocumentElement(l.floating)),boundary:f,rootBoundary:u,strategy:c})),v="floating"===m?{...a.floating,x:o,y:r}:a.reference,b=await(null==i.getOffsetParent?void 0:i.getOffsetParent(l.floating)),A=await(null==i.isElement?void 0:i.isElement(b))&&await(null==i.getScale?void 0:i.getScale(b))||{x:1,y:1},R=w(i.convertOffsetParentRelativeRectToViewportRelativeRect?await i.convertOffsetParentRelativeRectToViewportRelativeRect({rect:v,offsetParent:b,strategy:c}):v);return{top:(y.top-R.top+p.top)/A.y,bottom:(R.bottom-y.bottom+p.bottom)/A.y,left:(y.left-R.left+p.left)/A.x,right:(R.right-y.right+p.right)/A.x}}const R=t=>({name:"arrow",options:t,async fn(e){const{x:n,y:r,placement:i,rects:a,platform:c,elements:u,middlewareData:g}=e,{element:p,padding:h=0}=s(t,e)||{};if(null==p)return{};const y=x(h),w={x:n,y:r},v=d(i),b=m(v),A=await c.getDimensions(p),R="y"===v,P=R?"top":"left",D=R?"bottom":"right",E=R?"clientHeight":"clientWidth",T=a.reference[b]+a.reference[v]-w[v]-a.floating[b],O=w[v]-a.reference[v],L=await(null==c.getOffsetParent?void 0:c.getOffsetParent(p));let k=L?L[E]:0;k&&await(null==c.isElement?void 0:c.isElement(L))||(k=u.floating[E]||a.floating[b]);const B=T/2-O/2,C=k/2-A[b]/2-1,H=o(y[P],C),S=o(y[D],C),F=H,M=k-A[b]-S,V=k/2-A[b]/2+B,W=l(F,V,M),j=!g.arrow&&null!=f(i)&&V!=W&&a.reference[b]/2-(V<F?H:S)-A[b]/2<0,z=j?V<F?V-F:V-M:0;return{[v]:w[v]+z,data:{[v]:W,centerOffset:V-W-z,...j&&{alignmentOffset:z}},reset:j}}});const P=function(t){return void 0===t&&(t={}),{name:"autoPlacement",options:t,async fn(e){var o,r,i;const{rects:a,middlewareData:l,placement:u,platform:m,elements:g}=e,{crossAxis:d=!1,alignment:y,allowedPlacements:x=n,autoAlignment:w=!0,...v}=s(t,e),b=void 0!==y||x===n?function(t,e,n){return(t?[...n.filter((e=>f(e)===t)),...n.filter((e=>f(e)!==t))]:n.filter((t=>c(t)===t))).filter((n=>!t||f(n)===t||!!e&&h(n)!==n))}(y||null,w,x):x,R=await A(e,v),P=(null==(o=l.autoPlacement)?void 0:o.index)||0,D=b[P];if(null==D)return{};const E=p(D,a,await(null==m.isRTL?void 0:m.isRTL(g.floating)));if(u!==D)return{reset:{placement:b[0]}};const T=[R[c(D)],R[E[0]],R[E[1]]],O=[...(null==(r=l.autoPlacement)?void 0:r.overflows)||[],{placement:D,overflows:T}],L=b[P+1];if(L)return{data:{index:P+1,overflows:O},reset:{placement:L}};const k=O.map((t=>{const e=f(t.placement);return[t.placement,e&&d?t.overflows.slice(0,2).reduce(((t,e)=>t+e),0):t.overflows[0],t.overflows]})).sort(((t,e)=>t[1]-e[1])),B=(null==(i=k.filter((t=>t[2].slice(0,f(t[0])?2:3).every((t=>t<=0))))[0])?void 0:i[0])||k[0][0];return B!==u?{data:{index:P+1,overflows:O},reset:{placement:B}}:{}}}},D=function(t){return void 0===t&&(t={}),{name:"flip",options:t,async fn(e){var n,o;const{placement:r,middlewareData:i,rects:a,initialPlacement:l,platform:u,elements:m}=e,{mainAxis:g=!0,crossAxis:d=!0,fallbackPlacements:x,fallbackStrategy:w="bestFit",fallbackAxisSideDirection:v="none",flipAlignment:b=!0,...R}=s(t,e);if(null!=(n=i.arrow)&&n.alignmentOffset)return{};const P=c(r),D=c(l)===l,E=await(null==u.isRTL?void 0:u.isRTL(m.floating)),T=x||(D||!b?[y(l)]:function(t){const e=y(t);return[h(t),e,h(e)]}(l));x||"none"===v||T.push(...function(t,e,n,o){const r=f(t);let i=function(t,e,n){const o=["left","right"],r=["right","left"],i=["top","bottom"],a=["bottom","top"];switch(t){case"top":case"bottom":return n?e?r:o:e?o:r;case"left":case"right":return e?i:a;default:return[]}}(c(t),"start"===n,o);return r&&(i=i.map((t=>t+"-"+r)),e&&(i=i.concat(i.map(h)))),i}(l,b,v,E));const O=[l,...T],L=await A(e,R),k=[];let B=(null==(o=i.flip)?void 0:o.overflows)||[];if(g&&k.push(L[P]),d){const t=p(r,a,E);k.push(L[t[0]],L[t[1]])}if(B=[...B,{placement:r,overflows:k}],!k.every((t=>t<=0))){var C,H;const t=((null==(C=i.flip)?void 0:C.index)||0)+1,e=O[t];if(e)return{data:{index:t,overflows:B},reset:{placement:e}};let n=null==(H=B.filter((t=>t.overflows[0]<=0)).sort(((t,e)=>t.overflows[1]-e.overflows[1]))[0])?void 0:H.placement;if(!n)switch(w){case"bestFit":{var S;const t=null==(S=B.map((t=>[t.placement,t.overflows.filter((t=>t>0)).reduce(((t,e)=>t+e),0)])).sort(((t,e)=>t[1]-e[1]))[0])?void 0:S[0];t&&(n=t);break}case"initialPlacement":n=l}if(r!==n)return{reset:{placement:n}}}return{}}}};function E(t,e){return{top:t.top-e.height,right:t.right-e.width,bottom:t.bottom-e.height,left:t.left-e.width}}function T(e){return t.some((t=>e[t]>=0))}const O=function(t){return void 0===t&&(t={}),{name:"hide",options:t,async fn(e){const{rects:n}=e,{strategy:o="referenceHidden",...r}=s(t,e);switch(o){case"referenceHidden":{const t=E(await A(e,{...r,elementContext:"reference"}),n.reference);return{data:{referenceHiddenOffsets:t,referenceHidden:T(t)}}}case"escaped":{const t=E(await A(e,{...r,altBoundary:!0}),n.floating);return{data:{escapedOffsets:t,escaped:T(t)}}}default:return{}}}}};function L(t){const e=o(...t.map((t=>t.left))),n=o(...t.map((t=>t.top)));return{x:e,y:n,width:r(...t.map((t=>t.right)))-e,height:r(...t.map((t=>t.bottom)))-n}}const k=function(t){return void 0===t&&(t={}),{name:"inline",options:t,async fn(e){const{placement:n,elements:i,rects:a,platform:l,strategy:f}=e,{padding:u=2,x:m,y:d}=s(t,e),p=Array.from(await(null==l.getClientRects?void 0:l.getClientRects(i.reference))||[]),h=function(t){const e=t.slice().sort(((t,e)=>t.y-e.y)),n=[];let o=null;for(let t=0;t<e.length;t++){const r=e[t];!o||r.y-o.y>o.height/2?n.push([r]):n[n.length-1].push(r),o=r}return n.map((t=>w(L(t))))}(p),y=w(L(p)),v=x(u);const b=await l.getElementRects({reference:{getBoundingClientRect:function(){if(2===h.length&&h[0].left>h[1].right&&null!=m&&null!=d)return h.find((t=>m>t.left-v.left&&m<t.right+v.right&&d>t.top-v.top&&d<t.bottom+v.bottom))||y;if(h.length>=2){if("y"===g(n)){const t=h[0],e=h[h.length-1],o="top"===c(n),r=t.top,i=e.bottom,a=o?t.left:e.left,l=o?t.right:e.right;return{top:r,bottom:i,left:a,right:l,width:l-a,height:i-r,x:a,y:r}}const t="left"===c(n),e=r(...h.map((t=>t.right))),i=o(...h.map((t=>t.left))),a=h.filter((n=>t?n.left===i:n.right===e)),l=a[0].top,s=a[a.length-1].bottom;return{top:l,bottom:s,left:i,right:e,width:e-i,height:s-l,x:i,y:l}}return y}},floating:i.floating,strategy:f});return a.reference.x!==b.reference.x||a.reference.y!==b.reference.y||a.reference.width!==b.reference.width||a.reference.height!==b.reference.height?{reset:{rects:b}}:{}}}};const B=function(t){return void 0===t&&(t=0),{name:"offset",options:t,async fn(e){const{x:n,y:o}=e,r=await async function(t,e){const{placement:n,platform:o,elements:r}=t,i=await(null==o.isRTL?void 0:o.isRTL(r.floating)),a=c(n),l=f(n),u="y"===g(n),m=["left","top"].includes(a)?-1:1,d=i&&u?-1:1,p=s(e,t);let{mainAxis:h,crossAxis:y,alignmentAxis:x}="number"==typeof p?{mainAxis:p,crossAxis:0,alignmentAxis:null}:{mainAxis:0,crossAxis:0,alignmentAxis:null,...p};return l&&"number"==typeof x&&(y="end"===l?-1*x:x),u?{x:y*d,y:h*m}:{x:h*m,y:y*d}}(e,t);return{x:n+r.x,y:o+r.y,data:r}}}},C=function(t){return void 0===t&&(t={}),{name:"shift",options:t,async fn(e){const{x:n,y:o,placement:r}=e,{mainAxis:i=!0,crossAxis:a=!1,limiter:f={fn:t=>{let{x:e,y:n}=t;return{x:e,y:n}}},...m}=s(t,e),d={x:n,y:o},p=await A(e,m),h=g(c(r)),y=u(h);let x=d[y],w=d[h];if(i){const t="y"===y?"bottom":"right";x=l(x+p["y"===y?"top":"left"],x,x-p[t])}if(a){const t="y"===h?"bottom":"right";w=l(w+p["y"===h?"top":"left"],w,w-p[t])}const v=f.fn({...e,[y]:x,[h]:w});return{...v,data:{x:v.x-n,y:v.y-o}}}}},H=function(t){return void 0===t&&(t={}),{options:t,fn(e){const{x:n,y:o,placement:r,rects:i,middlewareData:a}=e,{offset:l=0,mainAxis:f=!0,crossAxis:m=!0}=s(t,e),d={x:n,y:o},p=g(r),h=u(p);let y=d[h],x=d[p];const w=s(l,e),v="number"==typeof w?{mainAxis:w,crossAxis:0}:{mainAxis:0,crossAxis:0,...w};if(f){const t="y"===h?"height":"width",e=i.reference[h]-i.floating[t]+v.mainAxis,n=i.reference[h]+i.reference[t]-v.mainAxis;y<e?y=e:y>n&&(y=n)}if(m){var b,A;const t="y"===h?"width":"height",e=["top","left"].includes(c(r)),n=i.reference[p]-i.floating[t]+(e&&(null==(b=a.offset)?void 0:b[p])||0)+(e?0:v.crossAxis),o=i.reference[p]+i.reference[t]+(e?0:(null==(A=a.offset)?void 0:A[p])||0)-(e?v.crossAxis:0);x<n?x=n:x>o&&(x=o)}return{[h]:y,[p]:x}}}},S=function(t){return void 0===t&&(t={}),{name:"size",options:t,async fn(e){const{placement:n,rects:i,platform:a,elements:l}=e,{apply:u=(()=>{}),...m}=s(t,e),d=await A(e,m),p=c(n),h=f(n),y="y"===g(n),{width:x,height:w}=i.floating;let v,b;"top"===p||"bottom"===p?(v=p,b=h===(await(null==a.isRTL?void 0:a.isRTL(l.floating))?"start":"end")?"left":"right"):(b=p,v="end"===h?"top":"bottom");const R=w-d[v],P=x-d[b],D=!e.middlewareData.shift;let E=R,T=P;if(y){const t=x-d.left-d.right;T=h||D?o(P,t):t}else{const t=w-d.top-d.bottom;E=h||D?o(R,t):t}if(D&&!h){const t=r(d.left,0),e=r(d.right,0),n=r(d.top,0),o=r(d.bottom,0);y?T=x-2*(0!==t||0!==e?t+e:r(d.left,d.right)):E=w-2*(0!==n||0!==o?n+o:r(d.top,d.bottom))}await u({...e,availableWidth:T,availableHeight:E});const O=await a.getDimensions(l.floating);return x!==O.width||w!==O.height?{reset:{rects:!0}}:{}}}};export{R as arrow,P as autoPlacement,b as computePosition,A as detectOverflow,D as flip,O as hide,k as inline,H as limitShift,B as offset,w as rectToClientRect,C as shift,S as size};
